package com.lnsoft.dqfs.server.util;

import io.netty.channel.Channel;
import io.netty.channel.socket.SocketChannel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *  下发目前不需要手动发起 暂时不需要用这个缓存
 */
public class CacheClient {
    private final Map<String, Channel> cacheMap = new ConcurrentHashMap<>();

    private static CacheClient instance = new CacheClient();

    public static CacheClient getInstance() {
        return instance;
    }

    private CacheClient() {
    }

    /**
     * @param clientId 数据库终端主键
     * @param channel  终端对应的SocketChannel
     * @return 如果以前这个链接存在返回以前的 并关闭就连接
     */
    public Channel cache(String clientId, Channel channel) {
        return cacheMap.put(clientId, channel);
    }

    public Channel cache(String clientId) {
        return cacheMap.get(clientId);
    }

    public void remove(String clientId) {
        if(clientId==null){
            return;
        }
        cacheMap.remove(clientId);
    }

}
